package gonggong;

import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import util.Constant;
import util.TotalClass;

import com.SmsModule.bean.TbSmsSend;
import com.SmsModule.dao.TbSmsSendDAO;
import com.dati.bean.Daan;
import com.dati.bean.DatiMiddle;
import com.dati.bean.SendDati;
import com.dati.dao.DatiDao;
import com.jifen.bean.JiFen;
import com.jifen.dao.JifenDao;

public class ScanSendDatiDao {
	private Statement st = null;
	private String sql = "";
	/**
	 *@author qingyu zhang
	 *@function:
	 *2011-4-13
	 *下午02:46:16
	 *zzClub
	 *gonggong
	 */
	public void operator(){
		getSendDati();
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:得到所有状态为1的即已发送但是没有验证答案的记录
	 * @return
	 *2011-4-13
	 *下午02:49:40
	 *zzClub
	 *gonggong
	 *List<SendDati>
	 */
	public void getSendDati(){
		String condition="state=1";
		List<SendDati> sendDatiList = new DatiDao().getSendDati("sendDati", "", condition);
		if(sendDatiList!=null&&sendDatiList.size()>0){
			for(SendDati sendDati:sendDatiList){
				List<TbSmsSend> smsSendList = new ArrayList<TbSmsSend>();
				TbSmsSend smsSend = new TbSmsSend();
				 SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm");
				 String timuId = sendDati.getAnswerMiddleId();
				 String phone = sendDati.getPhone();
				 String seriseId = sendDati.getSeriseId();
				 String conditionTimu="answerMiddleId='"+timuId+"'";
				 List<DatiMiddle> datiMiddleList = new DatiDao().getDatiMiddle("tb_answer_middle", "", conditionTimu);
				 String rightAnswer = datiMiddleList.get(0).getRightAnswer();//得到此题目的正确答案
				try {
					 Date nowDate = format.parse(Constant.decodeDateTT());
					 Date endDate =  format.parse(sendDati.getEndTime());
						
					 String daanCondition="phone='"+phone+"' and timuId='"+timuId+"' and state=0";
					 List<Daan> daanList = new DatiDao().getDaanList("tb_daan", daanCondition, "");
					 
					 JiFen jifen =  this.getJifen(seriseId, 1);
						String jifenValue = "0";
						if(jifen!=null){
							jifenValue=jifen.getIntegralValue();//得到此系列的积分
						}
						
						int totalNum = getSendDatiTotal(seriseId,phone,sendDati.getCreateTime(),sendDati.getEndTime());
					 if(nowDate.after(endDate)){//时间过期，则把此系列中所有标识为0或1 的修改成2
						//修改此系列中其他没有发送的信息的状态并得到总条数
						 List<SendDati> lst  = getSendDatiList(seriseId,phone,sendDati.getCreateTime(),sendDati.getEndTime());
						 int askNum = 0;
						 if(lst!=null&&lst.size()>0){
							 askNum=lst.size();
						 }
						 System.out.println("总数"+totalNum);
						 System.out.println("正确"+askNum);
						 String content = "你在规定的时间内没有回答完此系列答题,此系列有"+totalNum+"道题,回答"+askNum+"题.";
						 String rightJifen ="0";
						 if(daanList!=null&&daanList.size()>0)//回答正确
						 {
							 boolean ben = new GetJifen().modifyDaan(timuId, phone);//修改表中tb_daan中的状态
							 boolean bo = daanList.get(0).getDaAn().equals(rightAnswer); 
							 if(bo){
								 modifySendDati(sendDati.getId(),0);//成功
								 content+="此题回答正确.";
								 rightJifen = new GetJifen().getJiFen(jifenValue, totalNum,askNum+1);
									if(rightJifen.equals("0")){
										rightJifen="1";
									}
							 }else{
								 modifySendDati(sendDati.getId(),1);//失败
								 content+="此题回答错误,正确答案:"+rightAnswer;
								 rightJifen = new GetJifen().getJiFen(jifenValue, totalNum,askNum);
									if(rightJifen.equals("0")){
										rightJifen="1";
									}
							 }
							 //修改表tb_daan中的状态
						 }else{
							 modifySendDati(sendDati.getId(),1);//失败
							 content+="此题没有回答.";
							 rightJifen = new GetJifen().getJiFen(jifenValue, totalNum+askNum,askNum);
								if(rightJifen.equals("0")){
									rightJifen="1";
								}
						 }
						
						
						 content+="此次积分是:"+rightJifen+"分";
						 smsSend.setCreateTime(Constant.decodeDate());
						 smsSend.setDestAdd(phone);
						 smsSend.setIsSendTime(0);
						 smsSend.setSendTime(Constant.decodeDate());
						 smsSend.setSeriseId(seriseId+timuId);
						 smsSend.setSmsContent(content);
						 smsSend.setSmsSendState(0);
						 smsSend.setUserId(0);
						 smsSendList.add(smsSend);
						boolean bol =  new TbSmsSendDAO().addSmsList(smsSendList);//发送短信
						 if(bol){
							 boolean bool = new GetJifen().saveJifenToCustomer(phone, rightJifen);//存放积分
						 }
							System.out.println("过期content==="+content); 
					 }else{//时间不过期
						 String content = "此系列有"+totalNum+"题";
						 if(daanList!=null&&daanList.size()>0)//回答记录中存在此题目
						 {
							 boolean ben = new GetJifen().modifyDaan(timuId, phone);//修改表中tb_daan中的状态
							 boolean bo = daanList.get(0).getDaAn().equals(rightAnswer); 
							 if(bo){
								 modifySendDati(sendDati.getId(),0);//成功
								 content+="此题回答正确.";
							 }else{
								 modifySendDati(sendDati.getId(),1);//失败
								 content+="此题回答错误,正确答案:"+rightAnswer;
							 }
							 //从没有发送的系列题目中选取下一信息发送
							 List<SendDati> sendDatiLst = new GetJifen().getSendDati(seriseId, phone, sendDati.getEndTime(), 0, "");
							 List<SendDati> sendDatiLstOk = new GetJifen().getSendDati(seriseId, phone, sendDati.getEndTime(), 2, "0");//已经发送成功的
							 int okNum =0;
							 if(sendDatiLstOk!=null&&sendDatiLstOk.size()>0){
								 okNum=sendDatiLstOk.size();
							 }
							 if(sendDatiLst!=null&&sendDatiLst.size()>0){//此系列中存在其他答题
								 int num = new Random().nextInt(sendDatiLst.size());
								 SendDati sendDt = sendDatiLst.get(num);
								 String conditionTimuT="answerMiddleId='"+sendDt.getAnswerMiddleId()+"'";
								 List<DatiMiddle> datiMiddleLst = new DatiDao().getDatiMiddle("tb_answer_middle", "", conditionTimuT);
								 content+="下题:"+datiMiddleLst.get(0).getAnswerContent();
								 
								 //修改此题目的状态为1
								 boolean ble = modifySendDati(sendDt.getId());
							 }else{
									String rightJifen = new GetJifen().getJiFen(jifenValue, totalNum, okNum);
									if(rightJifen.equals("0")){
										rightJifen="1";
									}
									content+="此系列已答完.此次积分为"+rightJifen;
								 boolean bool = new GetJifen().saveJifenToCustomer(phone, rightJifen);//存放积分
							 }
							 TbSmsSend sms = new TbSmsSend();
							 sms.setCreateTime(Constant.decodeDate());
							 sms.setDestAdd(phone);
							 sms.setIsSendTime(0);
							 sms.setSendTime(Constant.decodeDate());
							 sms.setSeriseId("0"+seriseId+timuId);
							 sms.setSmsContent(content);//
							 sms.setSmsSendState(0);
							 sms.setUserId(0);
							 smsSendList.add(sms);
							System.out.println("不过期content==="+content); 
							 boolean bol =  new TbSmsSendDAO().addSmsList(smsSendList);//发送短信
						 }
					 }
					
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}//年-月-日  时：分
				
			}
		}
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:根据表tb_daan的ID修改其状态为1
	 * @param daanId
	 * @return
	 *2011-4-13
	 *下午05:00:30
	 *zzClub
	 *gonggong
	 *boolean
	 */
	public boolean modifyDaan(int daanId){
		boolean bo = false;
		sql="update tb_daan set state=1 where id="+daanId;
		st = new TotalClass().getStatement();
		try {
			bo = st.execute(sql);
			bo = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bo;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:修改此条记录是否正确、0:正确 1:失败
	 * @param sendDatiId
	 * @param rightOrno
	 * @return
	 *2011-4-13
	 *下午05:11:56
	 *zzClub
	 *gonggong
	 *boolean
	 */
	public boolean modifySendDati(int sendDatiId,int rightOrno){
		boolean bo = false;
		sql="update sendDati set rightOrno="+rightOrno+",state=2 where id="+sendDatiId;
		st = new TotalClass().getStatement();
		try {
			bo = st.execute(sql);
			bo = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bo ;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:当答题时间过时后。可根据系列ID修改此系列中没有发送的其他题目。。即状态（state）为1和0的。
	 * @param serise
	 * @return
	 *2011-4-13
	 *下午05:17:58
	 *zzClub
	 *gonggong
	 *boolean
	 */
	public int modifySendDati(String serise,String phone,String createTime,String endTime){
		boolean bo = false;
		int num = 0;
		String condition ="seriseId='"+serise+"' and phone='"+phone+"' and state in(1,0) and createTime='"+createTime+"' and endTime='"+endTime+"'";
		String sendDatiId = "";
		List<SendDati> sendDatiList = new DatiDao().getSendDati("sendDati", "", condition);
		if(sendDatiList!=null&&sendDatiList.size()>0){
			for(SendDati sendDati:sendDatiList){
				if(sendDatiId.equals("")){
					sendDatiId+=sendDati.getId();
				}else{
					sendDatiId+=","+sendDati.getId();
				}
				
			}
			num = sendDatiList.size();
		}
		if(!sendDatiId.equals("")){
			bo  = modifySendDati(sendDatiId);
		}
		return num ;
	}
	public int getSendDatiTotal(String serise,String phone,String createTime,String endTime){
		boolean bo = false;
		int num = 0;
		String condition ="seriseId='"+serise+"' and phone='"+phone+"' and state in(1,0,2) and createTime='"+createTime+"' and endTime='"+endTime+"'";
		String sendDatiId = "";
		List<SendDati> sendDatiList = new DatiDao().getSendDati("sendDati", "", condition);
		num = sendDatiList.size();
		return num ;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:在特定时间内得到此系列中回答正确的记录
	 * @param serise
	 * @param phone
	 * @param createTime
	 * @param endTime
	 * @param rightOrno
	 * @return
	 *2011-4-14
	 *上午09:07:11
	 *zzClub
	 *gonggong
	 *List<SendDati>
	 */
	public List<SendDati> getSendDatiList(String serise,String phone,String createTime,String endTime){
		String condition ="seriseId='"+serise+"' and phone='"+phone+"' and state in(1,0) and createTime='"+createTime+"' and endTime='"+endTime+"' and rightOrno=0";
		String sendDatiId = "";
		List<SendDati> sendDatiList = new DatiDao().getSendDati("sendDati", "", condition);
		return sendDatiList;
	}
	
	public boolean modifySendDati(String ids){
		boolean bo = false;
		sql = "update sendDati set state=2 ,rightOrno=1 where id in("+ids+")";
		st = new TotalClass().getStatement();
		try {
			bo = st.execute(sql);
			bo = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bo ;
	}
	/**
	 * 根据系列ID得到此系列上行或下行的分数0:上行   1:下行
	 *@author qingyu zhang
	 *@function:
	 * @param seriseId
	 * @param num
	 * @return
	 *2011-4-13
	 *下午09:33:13
	 *zzClub
	 *gonggong
	 *List<JiFen>
	 */
	public JiFen getJifen(String seriseId,int num){
		String condition="seriseId='"+seriseId+"' and integralType=1";
		JiFen jifen = new JiFen();
		jifen = new JifenDao().getJiFen("tb_integral", "", condition);
		return jifen;
	}
	
	/**
	 * 
	 *@author qingyu zhang
	 *@function:修改表sendDati状态为1
	 * @param sendDatiId
	 * @return
	 *2011-4-14
	 *上午08:36:04
	 *zzClub
	 *gonggong
	 *boolean
	 */
	public boolean modifySendDati(int sendDatiId){
		String sql = "update sendDati set state=1 where id="+sendDatiId;
		boolean bo  = new TotalClass().operatorObject(sql);
		return bo ;
	}
}
